package com.dartfar.news.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.news.model.News;
import com.dartfar.news.service.NewsService;
import com.dartfar.news.vo.NewsVo;

/**
 * 新闻Action对象
 * @author 王琦
 */

@Scope("prototype")
@Component("newsAction")
public class NewsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Service对象
	 */
	@Resource
	private NewsService newsService;
	
	/**
	 * 页面传参
	 */
	private int id;
	private String title;
	private String content;
	
	public String show() {
		//获取权限
		functions = getAutFunctions();
		return "show";
	}
	
	
	public String add(){
		News news = new News();
		news.setTitle(title);
		news.setContent(content);
		news.setPerson(curPerson().getName());
		news.setPersonId(String.valueOf(curPerson().getPersonId()));
		news.setDepart(curDepart().getName());
		news.setDepartId(String.valueOf(curDepart().getId()));
		news.setCreateDate(sdf.format(new Date()));
		news.setDate(new Date());
		newsService.save(news);
		return null;
	}
	
	public String edit(){
		News news = newsService.get(id);
		news.setTitle(title);
		news.setContent(content);
		newsService.update(news);
		return null;
	}
	
	public String remove(){
		newsService.delete(newsService.get(id));
		return null;
	}
	
	public String loadNewsData(){
		List<NewsVo> newsVos = new ArrayList<NewsVo>();
		DataGridVo<News> newsDataGridVo = new DataGridVo<News>();
		if (curUser() != null){
			List<News> list = newsService.getListByAut(autCode, curPerson(), curDepart(), pageNo, pageSize);	//查看全部
			Long newsCount = newsService.getCountByAut(autCode, curPerson(), curDepart());
			newsDataGridVo.setTotal(newsCount);
			newsDataGridVo.setRows(list);
		}
		String jsonResult = JSONObject.fromObject(newsDataGridVo).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}
	
	
	

}
